dnl configure.in
dnl
dnl Copyright (c) 2000 Dug Song <dugsong@monkey.org>
dnl
dnl $Id: configure.in,v 1.12 2000/05/17 17:51:32 dugsong Exp $

AC_INIT(dsniff.c)

AC_CONFIG_HEADER(config.h)

dnl Initialize prefix.
if test "$prefix" = "NONE"; then
   prefix="/usr/local"
fi

dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL

dnl Checks for header files.
AC_PATH_XTRA
AC_HEADER_STDC
AC_CHECK_HEADERS(err.h fcntl.h sys/ioctl.h unistd.h libgen.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T
AC_STRUCT_TM
dnl XXX - Solaris sux.
AC_CHECK_TYPE(u_int32_t, uint32_t)
dnl XXX - Linux sux.
CFLAGS="$CFLAGS -D_BSD_SOURCE"

dnl Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MEMCMP
AC_TYPE_SIGNAL
AC_FUNC_STRFTIME
AC_CHECK_FUNCS(gethostname socket strdup strstr)
dnl XXX - Solaris sux.
AC_CHECK_LIB(socket, socket)
AC_CHECK_LIB(nsl, gethostbyname)
dnl XXX - feh, everything except OpenBSD sux.
AC_REPLACE_FUNCS(dirname strlcpy strlcat)
needmd5=no
AC_CHECK_FUNCS(MD5Update, , [needmd5=yes])
if test $needmd5 = yes; then
   LIBOBJS="$LIBOBJS md5.o"
   MD5INC="-I\$(srcdir)/missing"
   AC_SUBST(MD5INC)
fi
neederr=no
AC_CHECK_FUNCS(warnx, , [neederr=yes])
if test $neederr = yes; then
   LIBOBJS="$LIBOBJS err.o"
fi
needethers=no
AC_CHECK_FUNCS(ether_ntoa, , [needethers=yes])
if test $needethers = yes; then
   LIBOBJS="$LIBOBJS ethers.o"
fi

dnl Checks for X11
if test "x$no_x" = "x"; then
   WEBSPY="webspy"; AC_SUBST(WEBSPY)
fi

dnl Checks for Berkeley DB
AC_MSG_CHECKING(for Berkeley DB with 1.85 compatibility)
AC_ARG_WITH(db,
[  --with-db=DIR           use Berkeley DB (with compat185) build directory],
[ case "$withval" in
  yes|no)
     AC_MSG_RESULT(no)
     ;;
  *)
     AC_MSG_RESULT($withval)
     if test -f $withval/build_unix/db_185.h -a \
	     -f $withval/build_unix/libdb.a -a \
	     -f $withval/build_unix/db185.o; then
        owd=`pwd`
        if cd $withval; then withval=`pwd`; cd $owd; fi
	AC_DEFINE(HAVE_DB_185_H)
	DBINC="-I$withval/build_unix"
	DBLIB="-L$withval/build_unix -ldb"
     else
        AC_ERROR(db_185.h or libdb.a not found in $withval or not configured with --enable-compat185)
     fi
     ;;
  esac ],
[ if test -f ${prefix}/include/db_185.h; then
     AC_DEFINE(HAVE_DB_185_H)
     DBINC="-I${prefix}/include"
     DBLIB="-L${prefix}/lib -ldb"
  elif test -f /usr/include/db_185.h; then
     AC_DEFINE(HAVE_DB_185_H)
     DBLIB="-ldb"
  elif test -f /usr/include/db.h; then
     AC_DEFINE(HAVE_DB_H)
  else
     AC_ERROR(Berkeley DB with 1.85 compatibility not found)
  fi
  AC_MSG_RESULT(yes) ]
)
AC_SUBST(DBINC)
AC_SUBST(DBLIB)

dnl Checks for libpcap
AC_MSG_CHECKING(for libpcap)
AC_ARG_WITH(libpcap,
[  --with-libpcap=DIR      use libpcap build directory],
[ case "$withval" in
  yes|no)
     AC_MSG_RESULT(no)
     ;;
  *)
     AC_MSG_RESULT($withval)
     if test -f $withval/pcap.h -a -f $withval/libpcap.a; then
        owd=`pwd`
        if cd $withval; then withval=`pwd`; cd $owd; fi
	PCAPINC="-I$withval -I$withval/bpf"
	PCAPLIB="-L$withval -lpcap"
     else
        AC_ERROR(pcap.h or libpcap.a not found in $withval)
     fi
     ;;
  esac ],
[ if test -f ${prefix}/include/pcap.h; then
     PCAPINC="-I${prefix}/include"
     PCAPLIB="-L${prefix}/lib -lpcap"
  elif test -f /usr/include/pcap/pcap.h; then
     PCAPINC="-I/usr/include/pcap"
     PCAPLIB="-lpcap"
  elif test -f /usr/include/pcap.h; then
     PCAPLIB="-lpcap"
  else
     AC_MSG_RESULT(no)
     AC_ERROR(libpcap not found)
  fi
  AC_MSG_RESULT(yes) ]
)
AC_SUBST(PCAPINC)
AC_SUBST(PCAPLIB)

dnl Checks for libnet
AC_MSG_CHECKING(for libnet)
AC_ARG_WITH(libnet,
[  --with-libnet=DIR       use libnet build directory],
[ case "$withval" in
  yes|no)
     AC_MSG_RESULT(no)
     ;;
  *)
     AC_MSG_RESULT($withval)
     if test -f $withval/include/libnet.h -a -f $withval/lib/libnet.a; then
        owd=`pwd`
        if cd $withval; then withval=`pwd`; cd $owd; fi
	CFLAGS="$CFLAGS `$withval/libnet-config --defines`"
	LNETINC="-I$withval/include"
	LNETLIB="-L$withval/lib -lnet"
     else
        AC_ERROR(libnet.h or libnet.a not found in $withval)
     fi
     ;;
  esac ],
[ if test -f ${prefix}/include/libnet.h; then
     CFLAGS="$CFLAGS `${prefix}/bin/libnet-config --defines`"
     LNETINC="-I${prefix}/include"
     LNETLIB="-L${prefix}/lib -lnet"
  elif test -f /usr/include/libnet.h; then
     CFLAGS="$CFLAGS `libnet-config --defines`"
     LNETLIB="-lnet"
  else
     AC_MSG_RESULT(no)
     AC_ERROR(libnet not found)
  fi
  AC_MSG_RESULT(yes) ]
)
AC_SUBST(LNETINC)
AC_SUBST(LNETLIB)

dnl Checks for libnids
AC_MSG_CHECKING(for libnids)
AC_ARG_WITH(libnids,
[  --with-libnids=DIR      use libnids build directory],
[ case "$withval" in
  yes|no)
     AC_MSG_RESULT(no)
     ;;
  *)
     AC_MSG_RESULT($withval)
     if test -f $withval/src/nids.h -a -f $withval/src/libnids.a; then
        owd=`pwd`
        if cd $withval; then withval=`pwd`; cd $owd; fi
	NIDSINC="-I$withval/src"
	NIDSLIB="-L$withval/src -lnids"
     else
        AC_ERROR(nids.h or libnids.a not found in $withval)
     fi
     ;;
  esac ],
[ if test -f ${prefix}/include/nids.h; then
     NIDSINC="-I${prefix}/include"
     NIDSLIB="-L${prefix}/lib -lnids"
  elif test -f /usr/include/nids.h; then
     NIDSLIB="-lnids"
  else
     AC_MSG_RESULT(no)
     AC_ERROR(libnids not found)
  fi
  AC_MSG_RESULT(yes) ]
)
save_cppflags="$CPPFLAGS"
CPPFLAGS="$NIDSINC"
AC_MSG_CHECKING(whether libnids version is good)
AC_EGREP_HEADER(pcap_filter, nids.h, AC_MSG_RESULT(yes), 
   [ AC_MSG_RESULT(no); AC_ERROR(libnids version 1.13 or greater required) ])
CPPFLAGS="$save_cppflags"
AC_SUBST(NIDSINC)
AC_SUBST(NIDSLIB)


AC_OUTPUT(Makefile)
